import { gunzip, gzip } from "./deps.ts";

export function readTextGz(fileName: string) {
  const bytes = gunzip(Deno.readFileSync(fileName));
  const data = new TextDecoder().decode(bytes);
  return data;
}

export function writeTextGz(fileName: string, data: string) {
  const bytes = new TextEncoder().encode(data);
  const compressed = gzip(bytes, undefined);
  Deno.writeFileSync(fileName, compressed);
}

export function readJsonGz<T>(fileName: string): T | null {
  const data = readTextGz(fileName);
  if (data) {
    return JSON.parse(data);
  }
  return null;
}

export function writeJsonGz(fileName: string, data: unknown, space = 0) {
  writeTextGz(fileName, JSON.stringify(data, null, space));
}
